package com.transfer.dao;

import com.transfer.Reservation;
import com.transfer.Room;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class RoomsDAO implements RoomsDAOLocal, Serializable {

    private static final Logger log = Logger.getLogger(RoomsDAOLocal.class.getName());
    @PersistenceContext(name = "MeetingRoomsPU")
    private EntityManager em;

    public List<Room> getAllRooms() {
        Query q = em.createNamedQuery("Room.findAll");
        return q.getResultList();
    }

    public void updateRoom(Room room) {
        Room old = em.find(Room.class, room.getId());
        old.setName(room.getName());
        old.setCapacity(room.getCapacity());
        em.merge(old);
    }

    public Room createRoom(String name, int capacity) {
        log.entering("RoomsDAO", "createRoom", new Object[]{name, capacity});
        Room room = new Room();
        room.setName(name);
        room.setCapacity(capacity);
        em.persist(room);
        return room;
    }

    public void updateReservation(Reservation reservation) {
        Reservation old = em.find(Reservation.class, reservation.getId());
        old.setContactPerson(reservation.getContactPerson());
        old.setNumberOfPeople(reservation.getNumberOfPeople());
        old.setResFrom(reservation.getResFrom());
        old.setResTo(reservation.getResTo());
        em.merge(old);
    }

    public Reservation createReservation(Room room) {
        Reservation res = new Reservation();
        res.setRoom(room);
        room.getReservations().add(res);
        em.persist(res);
        return res;
    }
}
